package com.my.aslife.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.my.aslife.module.entity.Ailyun;
import com.my.aslife.module.entity.Demand;
import com.my.aslife.utils.AddressUtils;
import com.my.aslife.utils.HttpUtils;
import com.my.aslife.utils.RegexUtils;
import com.my.aslife.utils.SerializeUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.http11.OutputFilter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * @author mayuan
 * @description TODO
 */
@SpringBootTest
@Slf4j
public class Main {

    public static void main(String[] args) {
        String host = "https://idenauthen.market.alicloudapi.com";
        String path = "/idenAuthentication";
        String method = "POST";
        String appcode = "cdb3d0d998964004919918fd9a2ee623";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("idNo", "51068220000730321X");
        bodys.put("name", "马源");


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            String s = EntityUtils.toString(response.getEntity());
            System.out.println(s);
            ObjectMapper objectMapper = new ObjectMapper();
            Ailyun ailyun = objectMapper.readValue(s, Ailyun.class);
            System.out.println(ailyun);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}